package com.lottery.im.module.chat.presenter;

import android.app.Activity;
import com.lottery.im.api.ApiClient;
import com.lottery.im.api.RetryWithDelay;
import com.lottery.im.api.observer.BaseObserver;
import com.lottery.im.api.result.ApiResult;
import com.lottery.im.api.result.entity.AdInfo;
import com.lottery.im.base.BaseFragmentPresenter;
import com.lottery.im.module.chat.view.ConversationListView;
import com.tencent.imsdk.TIMFriendshipManager;
import com.tencent.imsdk.TIMUserProfile;
import com.tencent.imsdk.TIMValueCallBack;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.FragmentEvent;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class ConversationListPresenter extends BaseFragmentPresenter<ConversationListView> {

  public ConversationListPresenter(Activity activity,
      ConversationListView iView,
      LifecycleProvider<FragmentEvent> provider) {
    super(activity, iView, provider);
  }

  public void getAd() {
    ApiClient.getApiRetrofitInstance()
        .getAD()
        .throttleFirst(2, TimeUnit.SECONDS)
        .retryWhen(new RetryWithDelay(1000, 10000))
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(provider.<ApiResult<AdInfo>>bindToLifecycle())
        .subscribe(new BaseObserver<AdInfo>() {
          @Override public void onStart() {

          }

          @Override public void onSuccess(AdInfo adInfo) {
            iView.getAdSuccess(adInfo);
          }

          @Override public void onFail(String code, String msg) {

          }

          @Override public void onException(String msg) {

          }

          @Override public void onFinish() {

          }
        });
  }

  /**
   * 获取用户信息，主要是获取非好友聊天的用户信息
   *
   * @param ids 用户ID列表
   */
  public void getUsersProfile(List<String> ids) {
    TIMFriendshipManager.getInstance().getUsersProfile(ids,
        new TIMValueCallBack<List<TIMUserProfile>>() {
          @Override public void onError(int i, String s) {

          }

          @Override public void onSuccess(List<TIMUserProfile> timUserProfiles) {
            if (null == iView) return;
            iView.addUsersProfile(timUserProfiles);
          }
        });
  }
}
